package com.example.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.entity.Student;
import com.example.demo.entity.SystemMenu;
import com.example.demo.entity.common.CommonResult;
import com.example.demo.service.SystemMenuService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * 系统菜单表(SystemMenu)表控制层
 *
 * @author makejava
 * @since 2020-05-27 16:58:52
 */
@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    /**
     * 服务对象
     */
    @Resource
    private SystemMenuService systemMenuService;

    @RequestMapping("toList")
    public String toList(){
        return "menu/menu_list";
    }

    @PostMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println(ids);
        return systemMenuService.deleteById(Arrays.asList(ids));
    }

    @PostMapping("deleteById1")
    @ResponseBody
    public boolean deleteById1(Integer id) {
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return systemMenuService.deleteById1(id);
    }



    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        SystemMenu bean = systemMenuService.queryById(id);
        model.addAttribute("bean",bean);
        return "menu/menu_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new SystemMenu());
        return "menu/menu_add";
    }

    @RequestMapping("save")
    @ResponseBody
    public Object save(SystemMenu bean) {
        System.out.println("修改和添加学生数据");
        System.out.println(bean);
        boolean result = false;
        //判断账号是否存在
        //判断是添加还是修改
        if (bean.getId() != null) {
            result = systemMenuService.update(bean);
        } else {
            result = systemMenuService.insert(bean).getId() != null;
        }
        return result;
    }

    @GetMapping("menus")
    @ResponseBody
    public Map<String,Object> menus() {
        return this.systemMenuService.queryAll();
    }

    @PostMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, SystemMenu bean) {
        CommonResult<SystemMenu> result = new CommonResult<>();
        IPage<SystemMenu> ipage = systemMenuService.queryAllByLimit(page, limit, bean);
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        return result;
    }



}